package com.seamtop.cuber.web.controller.cuber;

import com.seamtop.cuber.common.base.DataObject;
import com.seamtop.cuber.common.entriy.CuberReducer;
import com.seamtop.cuber.common.entriy.JobRecord;
import com.seamtop.cuber.common.metadata.TableMetaData;
import com.seamtop.cuber.common.util.StringUtil;
import com.seamtop.cuber.web.service.JobRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by feng on 2015/9/20.
 */
@Controller("CuberInfoController")
public class CuberInfoController {

    @Autowired
    private JobRecordService jobRecordService;

    @RequestMapping("/cuber/findReducerList.shtml")
    public String getReducerInfo(HttpServletRequest request, HttpServletResponse response,
                                 ModelMap model) {
        HashMap<String,CuberReducer> cuberReducerMap = DataObject.getCuberReducerMap();
        List<CuberReducer> reducerList = new ArrayList<CuberReducer>();
        for(String s:cuberReducerMap.keySet()){
            CuberReducer reducer = cuberReducerMap.get(s);
            reducerList.add(reducer);
        }
        Collections.sort(reducerList, new Comparator<CuberReducer>() {
            public int compare(CuberReducer arg0, CuberReducer arg1) {
                return arg0.getReducerId().compareTo(arg1.getReducerId());
            }
        });
        model.addAttribute("reducerList",reducerList);
        return "/cuber/reducer_list";
    }

    @RequestMapping("/cuber/findJobList.shtml")
    public String findJobList(HttpServletRequest request, HttpServletResponse response,
                                 ModelMap model) {

        List<JobRecord> jobRecordList = null;
        String reducerId = request.getParameter("reducerId");
        if(!StringUtil.isEmpty(reducerId)){
            jobRecordList = jobRecordService.findJobListByReducer(reducerId);
        }else{
            jobRecordList = jobRecordService.findJobList();
        }
        model.addAttribute("jobRecordList",jobRecordList);
        return "/cuber/job_list";
    }


}
